/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.restaurant;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.wechat.entity.restaurant.FoodCategory;
import com.boxin.ims.modules.wechat.entity.restaurant.Restaurant;
import com.boxin.ims.modules.wechat.service.restaurant.FoodCategoryService;
import com.boxin.ims.modules.wechat.service.restaurant.RestaurantService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 食品,菜品Controller
 * @author Jakemanse
 * @version 2013-11-14
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/restaurant/foodCategory")
public class FoodCategoryController extends BaseController {

	private final Logger logger = Logger.getLogger(FoodCategoryController.class);
	@Autowired
	private FoodCategoryService foodCategoryService;
	
	@Autowired
	private RestaurantService restaurantService;
	
	@ModelAttribute
	public FoodCategory get(@RequestParam(required=false) Long id) {
		if (id != null){
			return foodCategoryService.get(id);
		}else{
			return new FoodCategory();
		}
	}
	
	@RequiresPermissions("wechat:restaurant:foodCategory:view")
	@RequestMapping(value = {"list", ""})
	public String list(FoodCategory foodCategory, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			foodCategory.setUser(user);
		}
        Page<FoodCategory> page = foodCategoryService.find(new Page<FoodCategory>(request, response), foodCategory); 
        model.addAttribute("page", page);
        model.addAttribute("user", user);
		return "modules/wechat/restaurant/foodCategoryList";
	}

	@RequiresPermissions("wechat:restaurant:foodCategory:view")
	@RequestMapping(value = "form")
	public String form(FoodCategory foodCategory, Model model) {
		User user = UserUtils.getUser();
		
		
		if(foodCategory.getUser() == null || foodCategory.getUser().getId() < 1){
			foodCategory.setUser(user);
		}
		
		
		List<Restaurant> restaurantList = restaurantService.findByUserId(user.getId());
		
		model.addAttribute("foodCategory", foodCategory);
		model.addAttribute("restaurantList", restaurantList);
		
		
		return "modules/wechat/restaurant/foodCategoryForm";
	}

	@RequiresPermissions("wechat:restaurant:foodCategory:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("picfile") MultipartFile picfile,FoodCategory foodCategory, Model model,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, foodCategory)){
			return form(foodCategory, model);
		}
		
		if(picfile != null && picfile.getOriginalFilename().length()>4){
			String foodPath = WechatUtils.getWechatFoodResourceSavePath(request) + File.separator;
			FileUtils.createDirectory(foodPath);
			String sname = picfile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_food_"+System.currentTimeMillis()+fileType;
			File file = new File(foodPath+fname);
			logger.debug("保存文件:"+foodPath+fname);
			try {
				picfile.transferTo(file);
				foodCategory.setPicName(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+foodPath+fname);
				e.printStackTrace();
			}
			
		}
		
		
		foodCategoryService.save(foodCategory);
		addMessage(redirectAttributes, "保存食品,菜品'" + foodCategory.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/restaurant/foodCategory/?repage";
	}
	
	@RequiresPermissions("wechat:restaurant:foodCategory:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		foodCategoryService.delete(id);
		addMessage(redirectAttributes, "删除食品,菜品成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/restaurant/foodCategory/?repage";
	}

}
